package net.entelijan.cobean.examples.showcase.config.text;

import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JButtonFixture;
import org.fest.swing.fixture.JTextComponentFixture;
import org.junit.Assert;
import org.junit.Test;

public class TextFestTestCase {

	@SuppressWarnings("unused")
	private static Log log = LogFactory.getLog(TextFestTestCase.class);

	@Test
	public void testInitialBinding() {
		ConfigBookTextPanel panel = new ConfigBookTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookTextShowcase sc = new ConfigBookTextShowcase();
			JTextComponentFixture idTextBox = frameFix.textBox("idTextField");
			JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			JTextComponentFixture subtitleTextBox = frameFix
					.textBox("subtitleTextField");
			JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			sc.init(panel);
			pause(200);
			idTextBox.requireText("0");
			titleTextBox.requireText("Der Oger");
			subtitleTextBox.requireText("");
			yearTextBox.requireText("1986");
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetTitleFromGUI() {
		ConfigBookTextPanel panel = new ConfigBookTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookTextShowcase sc = new ConfigBookTextShowcase();
			JTextComponentFixture titleTextBox = frameFix
					.textBox("titleTextField");
			JTextComponentFixture subtitleTextBox = frameFix
					.textBox("subtitleTextField");
			sc.init(panel);
			pause(200);
			titleTextBox.deleteText();
			titleTextBox.setText("Oger Der");
			subtitleTextBox.focus();
			pause(200);
			Assert.assertEquals("Oger Der", sc.getBook().getTitle());
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetYearFromGUI() {
		ConfigBookTextPanel panel = new ConfigBookTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookTextShowcase sc = new ConfigBookTextShowcase();
			JTextComponentFixture subtitleTextBox = frameFix
					.textBox("subtitleTextField");
			JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			sc.init(panel);
			pause(200);
			yearTextBox.deleteText();
			yearTextBox.setText("1111");
			subtitleTextBox.focus();
			pause(200);
			Assert.assertEquals(1111, sc.getBook().getYear().intValue());
		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetYearWithFailureFromGUI() {
		ConfigBookTextPanel panel = new ConfigBookTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookTextShowcase sc = new ConfigBookTextShowcase();
			JTextComponentFixture failureTextBox = frameFix
					.textBox("failureTextField");
			JTextComponentFixture subtitleTextBox = frameFix
					.textBox("subtitleTextField");
			JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			JButtonFixture resetButton = frameFix.button("resetButton");
			sc.init(panel);
			pause(200);
			yearTextBox.deleteText();
			yearTextBox.setText("hallo");
			subtitleTextBox.focus();
			pause(200);
			failureTextBox.requireText("'hallo' is not a valid integer");
			Assert.assertEquals(1986, sc.getBook().getYear().intValue());
			resetButton.click();
			pause(100);
			failureTextBox.requireText("");
			yearTextBox.requireText("1986");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testDefaultValue() {
		ConfigBookTextPanel panel = new ConfigBookTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			ConfigBookTextShowcase sc = new ConfigBookTextShowcase();
			JTextComponentFixture subtitleTextBox = frameFix
					.textBox("subtitleTextField");
			JTextComponentFixture yearTextBox = frameFix
					.textBox("yearTextField");
			JButtonFixture defaultValuesButton = frameFix
					.button("defaultValuesButton");
			sc.init(panel);
			pause(200);
			yearTextBox.deleteText();
			yearTextBox.setText("1111");
			subtitleTextBox.focus();
			pause(200);

			defaultValuesButton.click();
			pause(200);
			yearTextBox.requireText("1986");

		} finally {
			frameFix.cleanUp();
		}

	}

	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
